
package cn.edu.xhu.library_management.department.config;

import cn.edu.xhu.library_management.common.entity.ResultCode;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

// 解决办法
//网站：https://github.com/xiaoymin/swagger-bootstrap-ui/issues/396
// spring.mvc.pathmatch.matching-strategy=ant-path-matcher


@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    @Bean(value = "userApi")
    @Order(value = 1)
    public Docket groupRestApi() {
        // 配置全局响应类型
        HashSet<String> produce = new HashSet<>();
        produce.add("application/json");
        // 配置全局响应码
        List<Response> responsesCode = new ArrayList<>();
        Arrays.stream(ResultCode.values()).forEach(resultCode -> {
            responsesCode.add(
                    new ResponseBuilder().code(resultCode.getCode() + "").description(resultCode.getMsg()).build()
            );
        });
        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponses(HttpMethod.GET,responsesCode)
                .globalResponses(HttpMethod.POST,responsesCode)
                .globalResponses(HttpMethod.PUT,responsesCode)
                .globalResponses(HttpMethod.DELETE,responsesCode)
                .produces(produce)
                .apiInfo(groupApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.edu.xhu.library_management.department.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo groupApiInfo(){
        return new ApiInfoBuilder()
                .title("智慧图书馆在线文档")
                .description("<div style='font-size:14px;color:red;'>用于智慧图书馆项目在线API测试</div>")
                .termsOfServiceUrl("http://www.lib.mmj-love.xyz/")
                .version("1.0")
                .contact(new Contact("MMJ",null,"2108634016@qq.com"))
                .build();
    }


}